package ext.generic.doc.processors;

import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.*;
import com.ptc.netmarkets.util.misc.NmActionServiceHelper;
import com.ptc.netmarkets.util.misc.NmContext;
import com.ptc.netmarkets.util.misc.NmContextItem;
import com.ptc.windchill.enterprise.util.PartManagementHelper;

import java.util.*;

import org.apache.log4j.Logger;

import wt.doc.WTDocument;

import wt.fc.WTReference;
import wt.fc.collections.WTArrayList;
import wt.fc.collections.WTCollection;

import wt.log4j.LogR;

import wt.part.*;

import wt.session.SessionHelper;

import wt.util.WTException;

import ext.generic.part.GenericPartManagementHelper;

import wt.fc.Persistable;
import wt.fc.ReferenceFactory;

import wt.httpgw.URLFactory;

import wt.util.LocalizableMessage;
import wt.util.WTMessage;

/**
 * 普通类文档中粘贴相关零部件的方法类,响应普通类文档"粘贴相关零部件"的action
 */
public class GenericDocPartDescPasteRelationFormProcessor extends DefaultObjectFormProcessor
{

    private static final Logger log;
    protected String redirectURL;

    static
    {
        try
        {
            log = LogR.getLogger(getClassName());
        }
        catch (Exception exception)
        {
            throw new ExceptionInInitializerError(exception);
        }
    }

    public GenericDocPartDescPasteRelationFormProcessor()
    {
        redirectURL = null;
    }

    static String getClassName()
    {
        return GenericDocPartDescPasteRelationFormProcessor.class.getName();
    }

    /**
     * 普通类文档中粘贴相关零部件的具体方法
     */
    public FormResult doOperation(NmCommandBean nmcommandbean, List list) throws WTException
    {
        WTDocument wtdocument = (WTDocument) nmcommandbean.getPrimaryOid().getWtRef().getObject();
        Vector vector = new Vector();
        System.out.println("ENTER : GenericDocPartDescPasteRelationFormProcessor.paste");
        boolean flag = PartManagementHelper.getWcPDMMethodPref();
        WTArrayList wtarraylist = new WTArrayList();
        if (flag || !PartDocHelper.isReferenceDocument(wtdocument))
        {
            wtarraylist.add(wtdocument);
        }
        NmContext nmcontext = NmContext.fromString(nmcommandbean.getCompContext());
        Stack stack = nmcontext.getContextItems();
        int i = stack.size();
        boolean flag1 = false;
        for (int j = 0; j < i; j++)
        {
            NmContextItem nmcontextitem = (NmContextItem) stack.get(j);
            if (nmcontextitem.getAction().equalsIgnoreCase("relatedDocumentPartInstances"))
            {
                flag1 = true;
            }
        }

        FormResult formresult = new FormResult();
        formresult.setStatus(FormProcessingStatus.SUCCESS);
        if (wtarraylist.size() > 0)
        {
            List list1 = NmClipboardUtility.getClipboardItemsToPaste(nmcommandbean);
            if (PartManagementHelper.isClipboardEmpty(list1, nmcommandbean, formresult))
            {
                return formresult;
            }
            for (int k = 0; k < list1.size(); k++)
            {
                NmClipboardItem nmclipboarditem = (NmClipboardItem) list1.get(k);
                NmOid nmoid = NmClipboardUtility.getNmOid(nmclipboarditem);
                log.debug((new StringBuilder()).append("oid and type - ").append(nmoid).toString());
                if (nmoid == null)
                {
                    continue;
                }
                wt.fc.Persistable persistable = nmoid.getWtRef().getObject();
                if (PartManagementHelper.isSameContainer(persistable, wtdocument))
                {
                    if ((persistable instanceof WTPart) && !flag1)
                    {
                        WTPart wtpart = (WTPart) persistable;
                        if (PartManagementHelper.isNewInWorkspace(wtpart))
                        {
                            java.util.Locale locale1 = SessionHelper.getLocale();
                            Object aobj[] = null;
                            ArrayList arraylist = new ArrayList();
                            String s = wtpart.getName();
                            arraylist.add(s);
                            aobj = arraylist.toArray();
                            WTMessage wtmessage = new WTMessage("com.ptc.windchill.enterprise.part.partResource", "109", aobj);
                            FeedbackMessage feedbackmessage = new FeedbackMessage(FeedbackType.FAILURE, locale1, null, null, new LocalizableMessage[]
                                    { wtmessage });
                            formresult.addFeedbackMessage(feedbackmessage);
                            formresult.setStatus(FormProcessingStatus.FAILURE);
                            return formresult;
                        }
                        WTCollection wtcollection = GenericPartManagementHelper.createRelationship(wtarraylist, wtpart, false, true, false);
                        if (wtcollection.size() > 0)
                        {
                            vector.add(persistable);
                        }
                        continue;
                    }
                    if ((persistable instanceof WTProductInstance2) && flag1)
                    {
                        WTProductInstance2 wtproductinstance2 = (WTProductInstance2) persistable;
                        WTCollection wtcollection1 = GenericPartManagementHelper.createRelationship(wtarraylist, wtproductinstance2, false);
                        if (wtcollection1.size() == 2)
                        {
                            vector.add(persistable);
                        }
                    }
                    else
                    {
                        vector.add(persistable);
                    }
                }
                else
                {
                    vector.add(persistable);
                }
            }

            if (vector.size() != 0)
            {
                java.util.Locale locale = SessionHelper.getLocale();
                formresult.addFeedbackMessage(PartManagementHelper.getPasteFbMsg(FeedbackType.FAILURE, vector, locale));
                formresult.setStatus(FormProcessingStatus.SUCCESS);
            }
        }
        log.debug("EXIT : PartDocDescPasteRelation.paste)");
        super.doOperation(nmcommandbean, list);
        return formresult;
    }


    protected String getURL(Object obj) throws WTException
    {
        ReferenceFactory referencefactory = new ReferenceFactory();
        String s = referencefactory.getReferenceString((Persistable) obj);
        URLFactory urlfactory = new URLFactory();
        HashMap hashmap = new HashMap();
        hashmap.put("oid", s);
        if (obj instanceof WTPart)
        {
            hashmap.put("tlnType", "part");
            hashmap.put("tlnAction", "relatedPartsDocuments");
        }
        else
        {
            hashmap.put("tlnType", "instance");
            hashmap.put("tlnAction", "relatedPartInstanceDocuments");
        }
        String s1 = NmActionServiceHelper.service.getAction("object", "view").getUrl();
        redirectURL = urlfactory.getHREF(s1, hashmap, true);
        return redirectURL;
    }
}
